namespace Circle {
    export var room =
        {
            _state: RoomState.NORMAL,
            set state(val) {
                this._state = val;
                console.log(" == > room set", RoomState[val]);
            },
            get state() { return this._state }
        };


    export var localKey = {
        args_2015: "args_2015_",
    };

    /*
        args_2015 记录进入圈子的参数,本地存储
        uid : 玩家id
        owner_uid : 圈主id
        id : 圈子id
    */
    export var args_2015 = null;
    //是否需要存储
    export var tosave_2015 = false;

    export var load_args_2015 = function () {

        return JSON.parse(egret.localStorage.getItem(localKey.args_2015 + GameConfig.gameType + "_" + game.player.uid));
    };
    
    export var save_args_2015 = function () {

        if (args_2015) {
            egret.localStorage.setItem(localKey.args_2015 + GameConfig.gameType + "_" + game.player.uid, JSON.stringify(args_2015));
        }
    };

    export function del_args_2015() {
        egret.localStorage.removeItem(localKey.args_2015 + GameConfig.gameType + "_" + game.player.uid);
    }

    export var circleNokai = null;

    /*  args_2017 加入房间参数
        id : 圈子id
        owner_uid : 圈主id
        uid : 玩家id
        type : 游戏id
        pass : 0
        roomid : 房间id
     */
    export var args_2017 = null;

    export function parseCircleJoinRoom() {
        const circle: any = game.search.circle;
        if (circle) {
            let cur: any[] = circle.split("_");
            let obj: any = {};
            obj.id = cur[0];
            obj.owner_uid = cur[1];
            obj.uid = cur[2];
            obj.type = +cur[3];
            obj.pass = cur[4];
            args_2017 = obj;
        }
    }

    //圈子的基础参数
    export var circle_data =
        {
            //圈子名称
            name: null,

            //圈主名字
            zhu_name: null,

            //是否圈主
            ifown: null,
        };

    export var circle_send = function (message_id, args) {
        gameManager.socketManager.send(message_id, args);
    };


    export var circle_link_check = function (key: string, value: string) {

        if (key == "circle") {

            args_2017 = circle_link.get_obj(value);
        }

    };


    export var circle_link = {

        get_str: function (obj: any) {

            var args = "&circle=";

            for (var key in obj) {
                if (key !== "roomid") {
                    args += obj[key] + "_";
                }
            }

            args = args.substr(0, args.length - 1);

            return args;
        },

        get_obj: function (str: string) {
            var obj: any = {};
            var args = str.split("_");
            obj.id = args[0];
            obj.owner_uid = args[1];
            obj.uid = args[2];
            obj.type = +args[3];
            obj.pass = args[4];
            if (args[5]) obj.roomid = args[5];
            return obj;
        }
    };

    export var circle_share_title = "【1%】老友圈自动开房，点击即玩！";
    export var circle_share_des = "圈子ID：1%";

    //按钮的冷却点击 time 冷却的时间
    export var button_cold_click = function (button: any, time: number = 2000) {

        button.addEventListener(egret.TouchEvent.TOUCH_TAP, function (e: egret.TouchEvent) {
            if (!button["last_time"]) {
                button["last_time"] = egret.getTimer();
            } else {
                if (egret.getTimer() - button["last_time"] > time) {
                    button["last_time"] = egret.getTimer();
                } else {
                    e.stopImmediatePropagation();
                }
            }
        }, this, false, 100);

    };

    //返回老友圈主界面
    export var returnCircleMain = function () {

        if (room.state == RoomState.CIRCLE) {
            if (args_2015) {
                circle_send(
                    2015,
                    args_2015
                );
            }
        }
    };

}
